let logic = module.exports;
let gameLogic = require('./gameLogic');
let utils = require('../../util/utils');
let gameProto = require('./gameProto');

class aiLogic {
    constructor(gameRule, cardsType, scaleType) {
        this.gameRule = gameRule;
        this.gameLogic = new gameLogic(gameRule);
        this.gameLogic.setCardsType(cardsType, scaleType);
    }
    //抢庄ai计算 
    //chairID 玩家ID
    //handCards 手牌
    onGameRobBank(handCards, ruffianRecord, isGameRob) {
        if (isGameRob >= 0) {
            return 4;
        }
        let hand = handCards.slice();
        if (this.gameRule.sendCardType == gameProto.sendCardType.QUANAN) {
            let CardsType = this.gameLogic.getCardsType(hand);
            let NiuNumber = this.gameLogic.getNiuNumber(hand);
            //全暗 且 牛八以上抢4 
            //牛8以下随机
            if ((CardsType >= gameProto.cardsType.YOUNIU && NiuNumber >= 8) || CardsType >= gameProto.cardsType.NIUNIU) {
                return 4;
            } else {
                let time_Robot = utils.getRandomNum(0, 2);
                return time_Robot;
            }
        } else if (this.gameRule.sendCardType == gameProto.sendCardType.MING3AN2) {
            hand[3] = 0;
            hand[4] = 0;
            let sun = 0;
            let list = 0;
            for (let i = 0; i < hand.length - 2; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue == (ruffianRecord & 0x0f)) {
                    return 4;
                }
            }
            for (let i = 0; i < hand.length - 2; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue <= 10) {
                    sun += getCardValue;
                    list++;
                }
            }
            if (sun == 20 && list == 3) {
                return 4;
            }

            let CardsType = this.gameLogic.getCardsType(hand);
            //明三暗二 有牛 抢4 
            //没牛随机
            if (CardsType >= gameProto.cardsType.YOUNIU) {
                return 4;
            }
            // else {
            //     let time_Robot = utils.getRandomNum(0, 2);
            //     return time_Robot;
            // }
        } else if (this.gameRule.sendCardType == gameProto.sendCardType.MING4AN1 || this.gameRule.sendCardType == gameProto.sendCardType.MINGPAI) {
            hand[4] = 0;
            let sun = 0;
            let list = 0;
            for (let i = 0; i < hand.length - 1; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue == (ruffianRecord & 0x0f)) {
                    return 4;
                }
            }
            for (let i = 0; i < hand.length - 1; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue <= 10) {
                    sun += getCardValue;
                    list++;
                }
            }
            if (sun == 20 && list == 4) {
                for (let i = 0; i < hand.length - 1; i++) {
                    let getCardhand = hand.slice();
                    sun = 0;
                    if (getCardhand[i] != 0) {
                        getCardhand[i] = 0;
                    } else {
                        break;
                    }

                    for (let j = 0; j < hand.length - 1; j++) {
                        if (i == j) continue;
                        let getCardhand_tou = getCardhand.slice();
                        if (getCardhand_tou[j] != 0) {
                            getCardhand_tou[j] = 0;
                        } else {
                            break;
                        }
                        for (let k = 0; k < hand.length - 1; k++) {
                            if (getCardhand[k] != 0 && (getCardhand[k] & 0x0f) <= 10) {
                                sun += getCardhand[k] & 0x0f;
                            }
                        }
                        if (sun == 10) {
                            return 4;
                        }
                    }

                }

            }
            let True_Felse = false;
            for (let i = 0; i < hand.length - 1; i++) {
                let getCardhand = hand.slice();
                sun = 0;
                getCardhand[i] = 0;
                for (let j = 0; j < hand.length - 1; j++) {
                    if (getCardhand[j] != 0 && (getCardhand[j] & 0x0f) < 10) {
                        sun += getCardhand[j] & 0x0f;
                    }
                }
                if (sun % 10 == 0) {
                    True_Felse = true;
                    break;
                }
            }
            let getCardhand = hand.slice();
            let getCardhand_1 = Math.floor(getCardhand[0] / 16);
            let getCardhand_true = true;
            for (let i = 1; i < hand.length - 1; i++) {
                if (getCardhand_1 != Math.floor(getCardhand[i] / 16)) {
                    getCardhand_true = false;
                }
            }
            if (getCardhand_true) {
                return 4;
            }
            let CardsType = this.gameLogic.getCardsType(hand);
            let NiuNumber = this.gameLogic.getNiuNumber(hand);
            //明四暗1 有牛 抢4 
            //牛8以下随机
            if (((CardsType >= gameProto.cardsType.YOUNIU && NiuNumber >= 8) || CardsType >= gameProto.cardsType.NIUNIU) && True_Felse) {
                return 4;
            } else if (True_Felse) {
                return 4;
            }
        }
        return 0;
    }
    //下分计算 
    //handCards 手牌
    //robBankScales 抢庄倍数
    //canPourScores 下分倍数 要*10
    onGamePourScore(handCards, robBankScales, ruffianRecord, canPourScores, isGameRob) {
        if (isGameRob >= 0) {
            return canPourScores[canPourScores.length - 1] * 10;
        }
        let hand = handCards.slice();
        // let canPourScores = this.gameRule.canPourScores.slice()
        if (robBankScales >= 3) {
            robBankScales = (canPourScores.length - 1);
        }
        if (this.gameRule.sendCardType == gameProto.sendCardType.QUANAN) {
            let CardsType = this.gameLogic.getCardsType(hand);
            let NiuNumber = this.gameLogic.getNiuNumber(hand);
            //全暗 且 牛八以上抢4 
            //牛8以下随机
            if ((CardsType >= gameProto.cardsType.YOUNIU && NiuNumber >= 8) || CardsType >= gameProto.cardsType.NIUNIU) {
                return canPourScores[canPourScores.length - 1] * 10;
            } else {
                let time_Robot = utils.getRandomNum(0, robBankScales);
                return canPourScores[time_Robot] * 10;
            }
        } else if (this.gameRule.sendCardType == gameProto.sendCardType.MING3AN2) {
            hand[3] = 0;
            hand[4] = 0;
            let sun = 0;
            let list = 0;
            for (let i = 0; i < hand.length - 2; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue == (ruffianRecord & 0x0f)) {
                    let time_Robot = utils.getRandomNum(2, robBankScales);
                    return canPourScores[time_Robot] * 10;
                }
            }

            for (let i = 0; i < hand.length - 2; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue <= 10) {
                    sun += getCardValue;
                    list++;
                }
            }
            if (sun == 20 && list == 3) {
                let time_Robot = utils.getRandomNum(1, robBankScales);
                return canPourScores[time_Robot] * 10;
            }

            let CardsType = this.gameLogic.getCardsType(hand);
            //明三暗二 有牛 抢4 
            //随机
            if (CardsType >= gameProto.cardsType.YOUNIU) {
                let time_Robot = utils.getRandomNum(0, robBankScales);
                return canPourScores[time_Robot] * 10;
                //return canPourScores[3] * 10;
            }
            // else {
            //     let time_Robot = utils.getRandomNum(0, robBankScales);
            //     return canPourScores[time_Robot] * 10;
            // }
        } else if (this.gameRule.sendCardType == gameProto.sendCardType.MING4AN1 || this.gameRule.sendCardType == gameProto.sendCardType.MINGPAI) {
            hand[4] = 0;
            let sun = 0;
            let list = 0;
            for (let i = 0; i < hand.length - 1; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue == (ruffianRecord & 0x0f)) {
                    let time_Robot = utils.getRandomNum(2, robBankScales);
                    return canPourScores[time_Robot] * 10;
                }
            }

            for (let i = 0; i < hand.length - 1; i++) {
                let getCardValue = hand[i] & 0x0f;
                if (getCardValue <= 10) {
                    sun += getCardValue;
                    list++;
                }
            }
            if (sun == 20 && list == 4) {
                for (let i = 0; i < hand.length - 1; i++) {
                    let getCardhand = hand.slice();
                    sun = 0;
                    if (getCardhand[i] != 0) {
                        getCardhand[i] = 0;
                    } else {
                        break;
                    }

                    for (let j = 0; j < hand.length - 1; j++) {
                        if (i == j) continue;
                        let getCardhand_tou = getCardhand.slice();
                        if (getCardhand_tou[j] != 0) {
                            getCardhand_tou[j] = 0;
                        } else {
                            break;
                        }
                        for (let k = 0; k < hand.length - 1; k++) {
                            if (getCardhand[k] != 0 && (getCardhand[k] & 0x0f) <= 10) {
                                sun += getCardhand[k] & 0x0f;
                            }
                        }
                        if (sun == 10) {
                            let time_Robot = utils.getRandomNum(1, robBankScales);
                            return canPourScores[time_Robot] * 10;
                        }
                    }

                }

            }
            let True_Felse = false;
            for (let i = 0; i < hand.length - 1; i++) {
                let getCardhand = hand.slice();
                sun = 0;
                getCardhand[i] = 0;
                for (let j = 0; j < hand.length - 1; j++) {
                    if (getCardhand[j] != 0 && (getCardhand[j] & 0x0f) <= 10) {
                        sun += getCardhand[j] & 0x0f;
                    }
                }
                if (sun % 10 == 0) {
                    True_Felse = true;
                    break;
                }
            }
            let CardsType = this.gameLogic.getCardsType(hand);
            let NiuNumber = this.gameLogic.getNiuNumber(hand);
            //明四暗1 有牛 抢4 
            //牛8以下随机
            if (CardsType >= gameProto.cardsType.NIUNIU && True_Felse) {
                return canPourScores[canPourScores.length - 1] * 10;
            } else if (CardsType >= gameProto.cardsType.YOUNIU && NiuNumber >= 8 && True_Felse) {
                let time_Robot = utils.getRandomNum(1, robBankScales);
                return canPourScores[time_Robot] * 10;
            } else if (CardsType >= gameProto.cardsType.YOUNIU && NiuNumber >= 6 && True_Felse) {
                let time_Robot = utils.getRandomNum(0, robBankScales);
                return canPourScores[time_Robot] * 10;
            } else if (CardsType >= gameProto.cardsType.YOUNIU && NiuNumber >= 1 && True_Felse) {
                return canPourScores[0] * 10;
            }
        }
        return canPourScores[0] * 10;
    }

}
module.exports = aiLogic;